<!doctype html>
<html lang="en">
 <head>
	<meta charset="UTF-8">
	<title>Alarm history</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<link rel="stylesheet" href="../css/index-ui.css">
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
	<script type="text/javascript" src="../js2/loginCookies.js"></script>
 </head>
 <body onload="page_init()">	
	<!-- 搜索 -->
	<div class="alarm-page-search">
		<ul>
			<li>
				<span>ID：</span>
				<input class="easyui-numberbox" id="nAlmId" type="text" style="width:60px;" placeholder="alarmID"/>
			</li>
			<li>
					<tr>
						<span>time：</span>
						<td>
							<input class="easyui-datebox" id="sStartDay" style="width:100px;" data-options="sharedCalendar:'#cc'">
						</td>
						<td>&nbsp;~&nbsp;</td>
						<td>
							<input class="easyui-datebox" id="sEndDay" style="width:100px;" data-options="sharedCalendar:'#cc'">
						</td>
					</tr>
				<div id="cc" class="easyui-calendar"></div>
			</li>
			<li>
				<span>Level：</span>
				<select class="easyui-combobox" id="nLevel" name="state" style="width:100px;" panelHeight="auto">
					<option value="-1">All</option>
					<option value="0">Inform</option>
					<option value="1">Warn</option>
					<option value="2">Alarm</option>
					<option value="3">Intlks</option>
				</select>
			</li>
			<li>
        <tr>
          <td>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px;margin:0 10px 0 10px;" onclick="onQuery()">Query</a>
          </td>
        </tr>
        <tr>
          <td>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:''" style="width:80px;margin:0 10px 0 10px;" onclick="onReport()">Export</a>
          </td>
        </tr>
			</li>
			<div class="clearfix"></div>
		</ul>
	</div>
	<!-- 表格 easyui-datagrid  data-options="method:'get',singleSelect:true,pagination:true" -->
	<table id="alarmlogs" class="easyui-datagrid" title="" style="width:100%;height:88%" pagination="false">
		<thead>
			<tr>
				<th data-options="field:'id',width:50">ID</th>
				<th data-options="field:'enableTime',width:130">startTime</th>
				<th data-options="field:'clearTime',width:130">endTime</th>
				<th data-options="field:'level',width:60">Level</th>
				<th data-options="field:'equipId',hidden:true">EquipId</th>
				<th data-options="field:'desc'" style="width:620px;">Desc</th>
			</tr>
		</thead>
	</table>
	<div id="alm-pag-svr" style="background:#efefef;border:1px solid #ccc;"></div>
 </body>
<script language="javascript"> 
var eqn = null;
var count = 1;
function page_init()
{
	if (eqn == null)
		eqn = get_page_param('eq');
				
	$('#alm-pag-svr').pagination({
		pageSize:30,
		layout:['list','prev','manual','next']
	});
	
	$('#alarmlogs').datagrid({
	//rownumbers:'true',
	singleSelect:'true',
	lines:true,
	clickToEdit: false,
	dblclickToEdit: false,
	loadMsg: 'Loading Data ......',
	pagination : false,
	pageSize : 30,
	method:'get'
	});
	
	on_refresh_alarm_content(1, 30);
}

$('#alm-pag-svr').pagination({
    pageSize:30,
    afterPageText:'',
    displayMsg:'',
    onSelectPage:function(pageNumber, pageSize){
    	$(this).pagination('loading');
	    on_refresh_alarm_content(pageNumber, pageSize);
		$(this).pagination('loaded');
	}
});

function on_refresh_alarm_content(pageNumber, pageSize)
{
	if (ams_is_sim_mode()) {
	 	$.getJSON('almHistory.json', function(json_data) {
			$('#alarmlogs').datagrid({
				data:json_data
			})
		})
	} else {
		var almId = $("#nAlmId").val() == '' ? -1 : $("#nAlmId").val();
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("GetEquipmentAlarms"),		
			data:JSON.stringify({
				"equipId":eqn,
				"sStartDay":$("#sStartDay").val(),
				"sEndDay":$("#sEndDay").val(),
				"nLevel":$("#nLevel").val(),
				"nIndex":(pageNumber - 1) * pageSize,
				"nAlmId":almId,
				"nCount":pageSize
			}),
			dataType: 'json',
			success : function(datas) {
				console.log(JSON.parse(datas.d));
				$('#alarmlogs').datagrid('loadData',JSON.parse(datas.d));
				if(JSON.parse(datas.d).length == pageSize){
          count += pageSize;
          $('#alm-pag-svr').pagination({
					total:count
					});
				}
			},
			error: function (error) {
				console.log(error) 
			}
		});
	}
}

function onQuery(){
	count = 1;
	$('#alm-pag-svr').pagination({
		pageNumber:1,
		layout:['list','prev','manual','next']
	});
	on_refresh_alarm_content(1, 30);
}

function onReport(){	
	if (ams_is_sim_mode()) {
		$.messager.alert("Tip", "Export is ok.", "info");
	} else{
		var almId = $("#nAlmId").val() == '' ? -1 : $("#nAlmId").val();
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("GetEquipAlarmsReport"),
			data:JSON.stringify({
				"equipId":eqn,
				"sStartDay":$("#sStartDay").val(),
				"sEndDay":$("#sEndDay").val(),
				"nAlmId":almId,
				"nLevel":$("#nLevel").val()
			}),
			dataType: 'json',
			success: function (response) {
				var fileName = JSON.parse(response.d).name;
				if (fileName != ""){
					parent.location.href = 'http://' + window.location.host + '/Report/' + JSON.parse(response.d).name + ".csv"
				}
				else{
					$.messager.alert("Error", "Error: Failed to create report.", "error");
				}
			},
			error: function (error) {
				$.messager.alert("Error", "Error: " + error, "error");
			}
		});
	}
}
		  
$.fn.datebox.defaults.formatter = function(date){
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	return y+'-'+m+'-'+d;
};

$.fn.datebox.defaults.parser = function(s){
	var t = Date.parse(s);
	if (!isNaN(t)){
		return new Date(t);
	} else {
		return new Date();
	}
}

$(function () {
 $("#alarmlogs").datagrid({
     onDblClickRow: function (index, row) {
	     	if (ams_is_sim_mode()) {
					$.messager.alert("Tip", "Alarm desc.", "info");
					return;
				}
				
     		$('#alarmlogs').datagrid('selectRow', index);
       	var row = $('#alarmlogs').datagrid('getSelected');
       	$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("getEqAlarmOpTip"),
				data:JSON.stringify({
					"eqName":eqn,
					"almId":row.id
				}),
				dataType: 'json',
				success: function (response) {
					console.log(JSON.parse(response.d))
					var icon = "info";
         	if (row.level == "Inform"){
         		icon = "info";
         	}
         	else if (row.level == "Warn"){
         		icon = "warning";
         	}
         	else{
         		icon = "error";
         	}
         	var jsonData = JSON.parse(response.d);
         	var title = jsonData.title;
         	var info = "";
         	if (title == "" && jsonData.rows.length == 0){
         		title = "注意";
         		info = "此报警没有详细描述信息。";
         	}
         	else{
         		for (var i = 0; i < jsonData.rows.length; i++){
	         		if (i > 0){
	         			info += "<br>";
	         		}        		
	         		info += jsonData.rows[i].info;
	         	}
	        }
         	$.messager.alert(title, info, icon);
				},
				error: function (error) {
					$.messager.alert("Error", "Error: " + error, "error");
				}
			});
     }
 });
})
</script>
</html>